package cn.ex.basic.controller;

import cn.ex.basic.customexception.BusinessException;
import cn.ex.basic.service.IVerifyCodeService;
import cn.ex.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author: ZeWangmu
 * @Date: 2022/06/15/7:26
 * @Description:
 * 生成图形验证码的接口
 */

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 1.图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imageCode(@PathVariable("imageCodeKey") String key){
        try {
            String codeStr = verifyCodeService.imageCode(key);
            return AjaxResult.me().setResultObj(codeStr);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
}
